package com.globant.google.mendoza.malbec;

import com.thoughtworks.selenium.DefaultSelenium;

/**
 * Helper class.
 */
public final class SeleniumUtil {

  /**
   * 1000 Milliseconds = 1 second.
   */
  private static final int ONE_SECOND = 1000;

  /**
   * Private constructor.
   */
  private SeleniumUtil() {
  }

  /**
   * Looks for a text in a page, continuously until found or timeout is reached.
   * The cycle repeats every second.
   * @param text
   *          the text to find.
   * @param timeout
   *          the time to wait while looking.
   * @param seleniumRC The selenium remote control used to interact with the
   *  screen.
   * @return true when found or false when the timeout was reached.
   */
  public static boolean waitForTextPresent(final DefaultSelenium seleniumRC,
      final String text, final long timeout) {
    boolean found = false;
    for (int i = 0; i < timeout / 1000; i++) {
      if (!seleniumRC.isTextPresent(text)) {
        try {
          Thread.sleep(ONE_SECOND);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      } else {
        found = true;
        break;
      }
    }
    return found;
  }

  /**
   * Looks for an element in a page, continuously until found or timeout is
   * reached. The cycle repeats every second.
   * @param locator
   *          the text used to locate the element to find.
   * @param timeout
   *          the time to wait while looking.
   * @param seleniumRC The selenium remote control used to interact with the
   *  screen.
   * @return true when found or false when the timeout was reached.
   */
  public static boolean waitForElementPresent(final DefaultSelenium seleniumRC,
      final String locator, final long timeout) {
    boolean found = false;
    for (int i = 0; i < timeout / 1000; i++) {
      if (!seleniumRC.isElementPresent(locator)) {
        try {
          Thread.sleep(ONE_SECOND);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      } else {
        found = true;
        break;
      }
    }
    return found;
  }

  /**
   * Looks for any of the elements in a page, continuously until one is found or
   *  timeout is reached. The cycle repeats every second.
   * @param locators
   *          the array containing the text used to locate the elements to find.
   * @param timeout
   *          the time to wait while looking.
   * @param seleniumRC The selenium remote control used to interact with the
   *  screen.
   * @return the locator of the element when one is found or "" when nothing is
   *  found.
   */
  public static String waitForElementsPresent(final DefaultSelenium seleniumRC,
      final String[] locators, final long timeout) {
    boolean found = false;
    int locator = 0;
    for (int i = 0; i < timeout / 1000 && !found; i++) {
      for (int j = 0; j < locators.length; j++) {
        if (seleniumRC.isElementPresent(locators[j])) {
          found = true;
          locator = j;
          break;
        }
      }
      try {
          Thread.sleep(ONE_SECOND);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
    if (found) {
      return locators[locator];
    } else {
      return "";
    }
  }

}
